/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import e.odbo.data.dao.BaseDAO;
import com.openbravo.bean.Location;
import com.openbravo.bean.Product;
import com.openbravo.bean.StockDiary;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.serialize.serializer.read.SerializerReadDouble;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;

import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.bean.StockCurrent;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

/**
 * 
 * @author Administrator
 */
public class StockCurrentDAO extends BaseDAO<StockCurrent> {

	public static int STOCKCHANGETYPE_ADD=0;
	public static int STOCKCHANGETYPE_SUB=1;
	
	public StockCurrentDAO(I_Session s) {
		super(s);
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition( "STOCKCURRENT", new Field[] {
				new Field("ID", Datas.STRING, Formats.STRING),
				new Field("LOCATION", Datas.STRING, Formats.STRING,
						Location.class),
				new Field("PRODUCT", Datas.STRING, Formats.STRING,
						Product.class),
				new Field("ATTRIBUTESETINSTANCE_ID", Datas.STRING,
						Formats.STRING),
				new Field("UNITS", Datas.DOUBLE, Formats.DOUBLE),
				new Field("INDATE", Datas.TIMESTAMP,Formats.TIMESTAMP),
				new Field("INREASON", Datas.STRING,Formats.STRING),
				new Field("OUTDATE", Datas.TIMESTAMP,Formats.TIMESTAMP),
				new Field("OUTREASON", Datas.STRING,Formats.STRING)
		},new int[] { 0 });
	}

	@Override
	public void writeInsertValues(DataWrite dp, StockCurrent obj)
			throws BasicException {
		dp.setString(1, obj.getId());
		dp.setString(2, obj.getLocationId());
		dp.setString(3, obj.getProductId());
		dp.setString(4, obj.getAttrsetinstId());
		dp.setDouble(5, obj.getUnits());
		dp.setTimestamp(6,obj.getInDate());
		dp.setString(7,obj.getInReason());
		dp.setTimestamp(8,obj.getOutDate());
		dp.setString(9,obj.getOutReason());
	}

	public StockCurrent readValues(DataRead dr,StockCurrent obj) throws BasicException {
		if(obj==null)
			obj = new StockCurrent();
		obj.setId(dr.getString(1));
		obj.setLocationId(dr.getString(2));
		obj.setProductId(dr.getString(3));
		obj.setAttrsetinstId(dr.getString(4));
		obj.setUnits(dr.getDouble(5));
		obj.setInDate(dr.getTimestamp(6));
		obj.setInReason(dr.getString(7));
		obj.setOutDate(dr.getTimestamp(8));
		obj.setOutReason(dr.getString(9));
		return obj;
	}

	public final double findProductStock(String warehouse, String id,
			String attsetinstid) throws BasicException {
		PreparedSentence p = attsetinstid == null ? new PreparedSentence(
				s,
				"SELECT UNITS FROM STOCKCURRENT WHERE LOCATION = ? AND PRODUCT = ? AND ATTRIBUTESETINSTANCE_ID IS NULL",
				new SerializerWriteBasic(Datas.STRING, Datas.STRING),
				SerializerReadDouble.INSTANCE)
				: new PreparedSentence(
						s,
						"SELECT UNITS FROM STOCKCURRENT WHERE LOCATION = ? AND PRODUCT = ? AND ATTRIBUTESETINSTANCE_ID = ?",
						new SerializerWriteBasic(Datas.STRING, Datas.STRING,
								Datas.STRING), SerializerReadDouble.INSTANCE);

		Double d = (Double) p.find(warehouse, id, attsetinstid);
		return d == null ? 0.0 : d.doubleValue();
	}

	public void stockAdd(final StockDiary diary) throws BasicException {
		int updateresult; // si ATTRIBUTESETINSTANCE_ID is null
		if (diary.getProductAttSetInstId() == null) {
			updateresult = new PreparedSentence(
					s,
					"UPDATE STOCKCURRENT SET UNITS = (UNITS + ?),INDATE=?,INREASON=? WHERE LOCATION = ? AND PRODUCT = ? AND ATTRIBUTESETINSTANCE_ID IS NULL",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setTimestamp(2,diary.getDate());
					setString(3, diary.getReason());
					setString(4, diary.getLocation());
					setString(5, diary.getProductID());

				}
			});
		} else
			updateresult = new PreparedSentence(
					s,
					"UPDATE STOCKCURRENT SET UNITS = (UNITS + ?),INDATE=?,INREASON=? WHERE LOCATION = ? AND PRODUCT = ? AND ATTRIBUTESETINSTANCE_ID = ?",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setTimestamp(2,diary.getDate());
					setString(3, diary.getReason());
					setString(4, diary.getLocation());
					setString(5, diary.getProductID());
					setString(6, diary.getProductAttSetInstId());
				}
			});

		if (updateresult == 0) {
			StockCurrent c = new StockCurrent();
			c.setProductId(diary.getProductID());
			c.setAttrsetinstId(diary.getProductAttSetInstId());
			c.setLocationId(diary.getLocation());
			c.setUnits(diary.getMultiply());
			c.setInDate(diary.getDate());
			c.setInReason(diary.getReason());
			insert(c);
		}
	}

	public void stockSub(final StockDiary diary) throws BasicException {
		int updateresult; // si ATTRIBUTESETINSTANCE_ID is null
		if (diary.getProductAttSetInstId() == null) {
			updateresult = new PreparedSentence(
					s,
					"UPDATE STOCKCURRENT SET UNITS = (UNITS - ?),OUTDATE=?,OUTREASON=? WHERE LOCATION = ? AND PRODUCT = ? AND ATTRIBUTESETINSTANCE_ID IS NULL",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setTimestamp(2,diary.getDate());
					setString(3, diary.getReason());
					setString(4, diary.getLocation());
					setString(5, diary.getProductID());

				}
			});
		} else
			updateresult = new PreparedSentence(
					s,
					"UPDATE STOCKCURRENT SET UNITS = (UNITS - ?),OUTDATE=?,OUTREASON=? WHERE LOCATION = ? AND PRODUCT = ? AND ATTRIBUTESETINSTANCE_ID = ?",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setTimestamp(2,diary.getDate());
					setString(3, diary.getReason());
					setString(4, diary.getLocation());
					setString(5, diary.getProductID());
					setString(6, diary.getProductAttSetInstId());
				}
			});

		if (updateresult == 0) {
			StockCurrent c = new StockCurrent();
			c.setProductId(diary.getProductID());
			c.setAttrsetinstId(diary.getProductAttSetInstId());
			c.setLocationId(diary.getLocation());
			c.setUnits(diary.getMultiply());
			c.setOutDate(diary.getDate());
			c.setOutReason(diary.getReason());
			insert(c);
		}
	}
	
	@Override
	public Class getSuportClass() {
		return StockCurrent.class;
	}
}
